﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KaoWuYouLibs.Dal;
using KaoWuYouLibs.Model;
using System.Security.Cryptography;

namespace KaoWuYouLibs.Bll
{
    public class PersonalInfomationBusiness
    {
        WebUserAccess access = new WebUserAccess();

        public bool UpdatePassword(string Password, string AccountName)
        {
            try
            {
                MD5Cng md5 = new MD5Cng();
                string encriptedpwd = Encoding.ASCII.GetString(md5.ComputeHash(Encoding.ASCII.GetBytes(Password)));

                access.UpdatePassword(encriptedpwd, AccountName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public WebUserModel GetPersonalInformation(string AccountName, string Password)
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string encriptedpwd = Encoding.ASCII.GetString(md5.ComputeHash(Encoding.ASCII.GetBytes(Password)));
                return access.FindUserInformation(AccountName, encriptedpwd);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool RegisterUser(string AccountName, string Password, string Name, string Tel, string EMail, string Gender)
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string encriptedpwd = Encoding.ASCII.GetString(md5.ComputeHash(Encoding.ASCII.GetBytes(Password)));
                access.Register(AccountName, EMail, encriptedpwd, Name, Gender, Tel);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdateUserInfo(string AccountName, string EmailAddress, string Name, string Gender, string Tel)
        {
            try
            {               
                access.UpdateUserInfo(AccountName, EmailAddress, Name, Gender, Tel);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool LogIn(string AccountName, string Password)
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string encriptedpwd = Encoding.ASCII.GetString(md5.ComputeHash(Encoding.ASCII.GetBytes(Password)));

                if (access.FindUserByAccountNameAndPassword(AccountName, encriptedpwd))
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
